package ogmios.dialogs;
import ogmios.core.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.*;
import java.io.*;
import ogmios.components.OgmiosFilter;
import java.util.*;
import java.beans.*;
/**
 * FileChoosers that can use OgmiosFilters. All file choosers returned by one of OgmiosFileChooser's methods (i.e. getFileDialog, getFilteredDialog, getFolderDialog)
 * are initialized to and can change an internal current directory. For example, if a file dialog returned by getFileDialog is last in the folder /home/foo/documents
 * before closing, a folder dialog returned by getFolderDialog will open in /home/foo/documents.
 * <br>
 * If you want an OgmiosFileChooser that does not use this internal current directory, you can initialize your own OgmiosFileChooser, rather than using the aforementioned methods.
 */
public class OgmiosFileChooser extends JFileChooser
{
 private static volatile File currentDirectory = new File(System.getProperty("user.home"));
 public static final PropertyChangeListener SET_CURRENT_DIRECTORY = 
  new PropertyChangeListener()
  {
    public void propertyChange(PropertyChangeEvent e)
    {
       if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName()))
       {
          currentDirectory = (File)e.getNewValue();
        }
    }
  };
  public File getSelectedFile()
 {
   File selectedFile = super.getSelectedFile();
   FileFilter filter = getFileFilter();
   if (selectedFile != null && filter instanceof OgmiosFilter)
   {
     String name = selectedFile.getName();
     if ((name.contains(".") == false))
      selectedFile = new File(selectedFile.getParentFile(), name + ((OgmiosFilter)filter).extension);
   }

   return selectedFile;
 }

 public void approveSelection()
 {
   if (getDialogType() == SAVE_DIALOG)
   {
     File selectedFile = getSelectedFile();
     if ((selectedFile != null) && selectedFile.exists())
     {
       if (JOptionPane.showConfirmDialog(this, selectedFile.getName() + " already exists. Do you want to replace it?", "Overwrite", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.YES_OPTION)
        return;
     }
   }
   super.approveSelection();
 }

 public void setFileFilter(String extension)
 {
   for (FileFilter filter : getChoosableFileFilters())
    if (filter instanceof OgmiosFilter && ((OgmiosFilter)filter).extension.equals(extension))
    {
      setFileFilter(filter);
      return;
    }
 }

 /**
 * Returns an OgmiosFileChooser based off the passed string.
 *
 * @param    extensions     Extensions in the form of " .java .py .hs | 'Java Files' 'Python Files' 'Haskell Files' "
 */
 public static OgmiosFileChooser getFileDialog(String extensions)
 {
  if (extensions != null)
  {
   int bar = extensions.indexOf("|");
   if (bar != -1 && bar != extensions.length() -1)
   {
     String[] ends = extensions.substring(0, bar).trim().split("\\s+");
     List<String> descriptions = CommandManager.splitCommandLine(extensions.substring(bar+1));
     if (descriptions.size() == ends.length)
     {
       OgmiosFilter[] filters = new OgmiosFilter[ends.length];
       for (int i =0; i < ends.length; i++)
       {
         filters[i] = new OgmiosFilter(ends[i], descriptions.get(i));
       }
       return getFilteredFileDialog(filters);
     }
   }
   }
   return getFilteredFileDialog((OgmiosFilter[])null);
 }

 /**
 * Returns an OgmiosFileChooser with these filters
 */
 public static OgmiosFileChooser getFilteredFileDialog(OgmiosFilter[] filters)
 {
   OgmiosFileChooser fileChooser = new OgmiosFileChooser();
   fileChooser.setCurrentDirectory(currentDirectory);
   fileChooser.addPropertyChangeListener(SET_CURRENT_DIRECTORY);
   fileChooser.setMultiSelectionEnabled(true);
   if (filters != null)
    for (OgmiosFilter filter : filters)
    {
      fileChooser.addChoosableFileFilter(filter);
    }
    return fileChooser;
 }

 /**
 * Returns a JFileChooser specifically for searching folders.
 * <br>
 * The reason this does not return an OgmiosFileChooser, but isn't
 * moved to an, ostensibly, more appropriate class like OgmiosUtilities
 * is because the returned folder dialog will have its current directory
 * set to OgmiosFileChooser's internal current directory, and there is no need
 * for an OgmiosFileChooser because we do not need extension filtering. 
 */
 public static JFileChooser getFolderDialog()
 {
   JFileChooser folderDialog = new JFileChooser();
   folderDialog.setCurrentDirectory(currentDirectory);
   folderDialog.addPropertyChangeListener(SET_CURRENT_DIRECTORY);
   folderDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   return folderDialog;
 }
}
